package com.jrx.anytxn.investor.timer.job.queryCreditResult.config;

import com.jrx.anytxn.investor.bean.InvestorQueryCreditResultBean;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.TlInvestorLimitApproveLog;
import com.jrx.anytxn.investor.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.investor.timer.job.queryCreditResult.step.InvestorQueryCreditResultProcessor;
import com.jrx.anytxn.investor.timer.job.queryCreditResult.step.InvestorQueryCreditResultWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 查询合作行授信结果
 *
 * @author liuyang
 * @date 2020-06-09
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class InvestorQueryCreditResultConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private InvestorQueryCreditResultProcessor queryCreditResultProcessor;
    @Autowired
    private InvestorQueryCreditResultWriter queryCreditResultWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job queryCreditResultJob() {
        return jobBuilderFactory.get("queryCreditResultJob")
                .start(queryCreditResultStep())
                .build();
    }

    /**
     * 查询合作行授信结果流程处理
     *
     * @return Step
     * @author liuyang
     * @date 2020-06-09
     */
    @Bean
    public Step queryCreditResultStep() {
        return stepBuilderFactory.get("queryCreditResultStep")
                .<TlInvestorLimitApproveLog, InvestorQueryCreditResultBean>chunk(anytxnBatchProperties.getQueryCreditResultConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(queryCreditResultReader())
                .processor(queryCreditResultProcessor)
                .writer(queryCreditResultWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getQueryCreditResultConfig().getThrottleLimit())//多线程
                .build();
    }

    /**
     * 查询处理中的合作方审批记录
     *
     * @return ItemReader<TlInvestorLimitApproveLog> 合作方审批记录
     * @author liuyang
     * @date 2020-06-09
     */
    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlInvestorLimitApproveLog> queryCreditResultReader() {

        MyBatisCursorItemReader<TlInvestorLimitApproveLog> reader = new MyBatisCursorItemReader<>();
        reader.setSaveState(false);
        reader.setSqlSessionFactory(sqlSessionFactory);
        reader.setQueryId("com.jrx.anytxn.investor.mapper.ext.ExtTlInvestorLimitApproveLogMapper.selectByApproveStatusForBatch");
        Map<String, Object> map = new HashMap<>();
        map.put("approveStatusList", Arrays.asList(TransBizConstant.APPROVE_STATUS_O));
        reader.setParameterValues(map);

        SynchronizedItemStreamReader<TlInvestorLimitApproveLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(reader);
        return synchronizedItemStreamReader;
    }
}
